package idv.arthur.work;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class QueryApplyHistory extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public QueryApplyHistory() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/json");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String errPars = "}";
		String strSQL = null;
		ArrayList<QueryApply> qas = new ArrayList<QueryApply>();

		try {
			conn = wid.getConn(true);
			if ( conn.getMetaData().getDatabaseProductName().contains( "MySQL" ) ) {
				strSQL ="SELECT t1.deliverySN,t3.username,t2.createdDateTime,t4.sysname FROM (deltask t1,deplmain t2) LEFT JOIN personnel t3 ON (t2.creator=t3.userid) LEFT JOIN envsystemlist t4 ON (t1.tarSysSN=t4.sysSN) WHERE t1.isDeliver='t' AND t1.deplmainSN=t2.deplmainSN ORDER BY t1.deliverySN DESC";
			} else if ( conn.getMetaData().getDatabaseProductName().contains( "Informix" ) ) {
				strSQL ="SELECT t1.deliverySN,t3.username,t2.createdDateTime,t4.sysname FROM deltask t1,deplmain t2, personnel t3, OUTER envsystemlist t4 WHERE t2.creator=t3.userid AND t1.tarSysSN=t4.sysSN AND t1.isDeliver='t' AND t1.deplmainSN=t2.deplmainSN ORDER BY t1.deliverySN DESC";				
			}
			
			ps = conn.prepareStatement(strSQL);
			rs = ps.executeQuery();
			while (rs.next()) {
				QueryApply qa = new QueryApply();
				qa.setDeliverySN(rs.getInt("deliverySN"));
				qa.setCreateDateTime( wid.changeTimeFormat( rs.getTimestamp("createdDateTime") ) );
				qa.setTarSysName( wid.DB2UI( rs.getString("sysname"),conn.getMetaData().getDatabaseProductName() ) );
				qa.setUserName( wid.DB2UI( rs.getString("username"),conn.getMetaData().getDatabaseProductName() ) );
				qas.add(qa);
			}
		} catch ( SQLException e) {
			wid.kem(conn, ps, e, wid.toEType("SQLException")+errPars, wid.isPrint2Console );
		} catch ( UnsupportedEncodingException e) {
			wid.kem(conn, ps, e, wid.toEType("UnsupportedEncodingException")+errPars, wid.isPrint2Console );
		} catch ( Exception e) {
			wid.kem(conn, ps, e, wid.toEType("Exception")+errPars, wid.isPrint2Console );
		} finally {
			try {
				if (rs != null) {rs.close();}
				if (ps != null) {ps.close();}
				if (conn != null) {conn.close();}	
			} catch (SQLException e ) {
				System.out.println(wid.errProcess(e));
			}
		}
		
		response.getWriter().write(new Gson().toJson(qas));
	}
}